angular.module('starter.controllers')
.controller('CheckpayCtrl', function($scope, museumFac, $stateParams, $timeout, $ionicScrollDelegate, cfg, $location, $cookies, $interval, $state, uploadImg) {
  	$scope.payid = $stateParams.id;
    $scope.pay_order = {};

    $scope.backhome = function(){
      $location.url('home');
    }

    $scope.gotoCenter = function(){
      museumFac.user.usertest(function(data){
        if(data.info == true){
          $location.url('center');
        }else{
          $scope.url = window.location.href;
              $cookies.put('lastPage', $scope.url);
          $location.url('login');
        }
      });
    }

    // 中英文切换
    $scope.change_language = function(id){
      $scope.language = id;
      var expireDate = new Date();  
      expireDate.setDate(expireDate.getDate() + 30);                
      $cookies.put('language', id, {'expires': expireDate.toUTCString()});
      location.reload();
      if(id == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }

    }

    // 检测当前语种
    $scope.getLanguage = function(){
      $scope.language = $cookies.get('language');
      if($scope.language){
          $scope.language = $scope.language;
      }else{
          $scope.language = 0;
      }
      if($scope.language == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }
    }
    $scope.getLanguage();

    // 是否注册用户
    $scope.checkOpenid = function(){

      museumFac.user.usertest(function(data){
          $scope.is_login = data.info;

          if($scope.is_login == false){
              $scope.url = cfg.baseUrl + 'home/index/checkpay?orderId=' + $stateParams.id;
              $cookies.put('lastPage', $scope.url);
              $location.url('login');
            }

        },function(data){
          alert(data.data.info);
      });
    }
    $scope.checkOpenid();

    $scope.getUser = function() {
      museumFac.user.profile(function(data){
        $scope.user2 = data.info;
      },function(data){
        alert(data.data.info);
      });
    };
    $scope.getUser();

    $scope.getaddress = function(){
        museumFac.product.addresses(function(data){
            $scope.addresses = data.info;
            $scope.getOrder();
          },function(data){
            alert(data.data.info);
        });
    }
    $scope.getaddress();

    // 获取订单信息
    $scope.getOrder = function(){
      $scope.order = [];

      if($stateParams.id != 0){

        museumFac.product.paydetail({
          orderId:$stateParams.id
        },function(data){
            $scope.order = data.info;
            if($scope.order.address_id){
              if($scope.order.province == 1||$scope.order.province == 20||$scope.order.province == 860||$scope.order.province == 2462){
                  $scope.city_show_name = $scope.order.province_name + ' ' + $scope.order.district_name + ' ';
              }else{
                  $scope.city_show_name = $scope.order.province_name + ' ' + $scope.order.city_name + ' ' + $scope.order.district_name + ' ';
              }
            }else{
              if($scope.order.default_address){
                if($scope.order.default_address.province == 1||$scope.order.default_address.province == 20||$scope.order.default_address.province == 860||$scope.order.default_address.province == 2462){
                    $scope.city_show_name = $scope.order.default_address.province_name + ' ' + $scope.order.default_address.district_name + ' ';
                }else{
                    $scope.city_show_name = $scope.order.default_address.province_name + ' ' + $scope.order.default_address.city_name + ' ' + $scope.order.default_address.district_name + ' ';
                }
              }
            }

            $scope.allprice = 0;
            $scope.allquantity = 0;
            angular.forEach($scope.order.products, function(item){
              item.quantity = parseInt(item.quantity, 10);
              if(item.activity_price != 0){
                $scope.allprice = Math.round(($scope.allprice + item.activity_price*item.quantity)*100)/100;
              }else{
                $scope.allprice = Math.round(($scope.allprice + item.price*item.quantity)*100)/100;
              }
              
              $scope.allquantity = $scope.allquantity + item.quantity;
            });
          },function(data){
            alert(data.data.info);
        });

      }

      if($stateParams.id == 0){

        museumFac.product.mycart(function(data){
            $scope.orders = data.info;

            $scope.order.products = $scope.orders;

            angular.forEach($scope.addresses, function(item){
              // console.log(item);
              if(item.is_default == 1){
                $scope.order.default_address = item;

                if($scope.order.default_address.province == 1||$scope.order.default_address.province == 20||$scope.order.default_address.province == 860||$scope.order.default_address.province == 2462){
                    $scope.city_show_name = $scope.order.default_address.province_name + ' ' + $scope.order.default_address.district_name + ' ';
                }else{
                    $scope.city_show_name = $scope.order.default_address.province_name + ' ' + $scope.order.default_address.city_name + ' ' + $scope.order.default_address.district_name + ' ';
                }

              }
            });
            // console.log($scope.city_show_name);

            $scope.height2 = $('.cart').height();

            $scope.allprice = 0;
            $scope.allquantity = 0;
            angular.forEach($scope.order.products, function(item){
              item.quantity = parseInt(item.quantity, 10);
              if(item.activity_price != 0){
                $scope.allprice = Math.round(($scope.allprice + item.activity_price*item.quantity)*100)/100;
              }else{
                $scope.allprice = Math.round(($scope.allprice + item.price*item.quantity)*100)/100;
              }
              
              $scope.allquantity = $scope.allquantity + item.quantity;
            });
          },function(data){
            alert(data.data.info);
        });

      }

    	
  	}
  	// $scope.getOrder();

    $scope.show_placelist = function(){
      $scope.place1 = 1;
    }

    $scope.gotonew = function(){
      $scope.place1 = 2;
      $scope.user = {};
    }

    $scope.delete = function(list){
        museumFac.product.deleteaddress({
            addressId:list.id
        },function(data){
            $scope.getaddress();
            $scope.cancel_mark = 0;
          },function(data){
            alert(data.data.info);
        });

        event.stopPropagation();
    }

    // 新增地址
    $scope.user = {};

    $scope.choen = function(flag){
        $scope.inputc = flag;
        if(flag == 3){
            $scope.getprovince();
            $scope.mubu1 = 1;
        }
    }

    $scope.getprovince = function(){
        museumFac.product.cities(function(data){
            $scope.citys = data.info;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.cancle_city = function(){
        $scope.mubu1 = 0;
    }

    $scope.cancle_city1 = function(){
        $scope.mubu1 = 1;
    }

    $scope.cancle_city2 = function(){
        $scope.mubu1 = 2;
    }

    $scope.showcity = function(list){
        $scope.province_name = list.name;
        $scope.province_id = list.id;
        museumFac.product.cities({
            pid:list.id
        },function(data){
            $scope.citys1 = data.info;
            $scope.mubu1 = 2;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.showcity1 = function(list){
        $scope.city_name = list.name;
        $scope.city_id = list.id;
        museumFac.product.cities({
            pid:list.id
        },function(data){
            $scope.citys2 = data.info;
            $scope.mubu1 = 3;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.showcity2 = function(list){
        $scope.district_name = list.name;
        $scope.district_id = list.id;
        $scope.mubu1 = 0;
        if($scope.province_id == 1||$scope.province_id == 20||$scope.province_id == 860||$scope.province_id == 2462){
            $scope.city_show_name1 = $scope.province_name + ' ' + $scope.district_name;
        }else{
            $scope.city_show_name1 = $scope.province_name + ' ' + $scope.city_name + ' ' + $scope.district_name;
        } 
    }

    $scope.addaddress = function(){
        $scope.user.province = $scope.province_id;
        $scope.user.city = $scope.city_id;
        $scope.user.district = $scope.district_id;
        museumFac.product.addaddress($scope.user,function(data){
            alert('添加地址成功');
            $scope.place1 = 1;
            $scope.getaddress();
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.changecheck = function(){
        if($scope.user.is_default == 1){
            $scope.user.is_default = 0;
        }else{
            $scope.user.is_default = 1;
        }
    }
    // 新增地址结束

    // 修改地址
    $scope.getinfo = function(){
        // museumFac.product.addressdetail({
        //     addressId:$stateParams.id
        // },function(data){
            if($scope.user.province_id == 1||$scope.user.province_id == 20||$scope.user.province_id == 860||$scope.user.province_id == 2462){
                $scope.city_show_name2 = $scope.user.province_name + ' ' + $scope.user.district_name;
            }else{
                $scope.city_show_name2 = $scope.user.province_name + ' ' + $scope.user.city_name + ' ' + $scope.user.district_name;
            }
        //   },function(data){
        //     alert(data.data.info);
        // });
    }

    $scope.gotoEdit = function(list){
        $scope.user = list;
        $scope.place1 = 3;
        $scope.getinfo();

        event.stopPropagation();
    }

    $scope.choen1 = function(flag){
        $scope.inputc = flag;
        if(flag == 3){
            $scope.getprovince1();
            $scope.mubu1 = 1;
        }
    }

    $scope.getprovince1 = function(){
        museumFac.product.cities(function(data){
            $scope.citys = data.info;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.cancle_city = function(){
        $scope.mubu1 = 0;
    }

    $scope.cancle_city1 = function(){
        $scope.mubu1 = 1;
    }

    $scope.cancle_city2 = function(){
        $scope.mubu1 = 2;
    }

    $scope.showcity_3 = function(list){
        $scope.province_name = list.name;
        $scope.province_id = list.id;
        museumFac.product.cities({
            pid:list.id
        },function(data){
            $scope.citys1 = data.info;
            $scope.mubu1 = 2;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.showcity1_3 = function(list){
        $scope.city_name = list.name;
        $scope.city_id = list.id;
        museumFac.product.cities({
            pid:list.id
        },function(data){
            $scope.citys2 = data.info;
            $scope.mubu1 = 3;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.showcity2_3 = function(list){
        $scope.district_name = list.name;
        $scope.district_id = list.id;
        $scope.mubu1 = 0;
        if($scope.province_id == 1||$scope.province_id == 20||$scope.province_id == 860||$scope.province_id == 2462){
            $scope.city_show_name2 = $scope.province_name + ' ' + $scope.district_name;
        }else{
            $scope.city_show_name2 = $scope.province_name + ' ' + $scope.city_name + ' ' + $scope.district_name;
        }
        $scope.user1.province = $scope.province_id;
        $scope.user1.city = $scope.city_id;
        $scope.user1.district = $scope.district_id;
    }

    $scope.cancleaddress = function(){
        $scope.place1 = 1;
    }

    $scope.changecheck1 = function(){
        if($scope.user.is_default == 0){
            $scope.user.is_default = 1;
            $scope.user1.is_default = $scope.user.is_default;
        }else{
            $scope.user.is_default = 0;
            $scope.user1.is_default = $scope.user.is_default;
        }
    }

    $scope.user1 = {};
    $scope.addaddress_3 = function(){
        $scope.user1.phone = $scope.user.phone;
        $scope.user1.name = $scope.user.name;
        $scope.user1.addressId = $scope.user.id;
        $scope.user1.area = $scope.user.area;
        $scope.user1.is_default = $scope.user.is_default;
        if(!$scope.user1.province){
            $scope.user1.province = $scope.user.province;
        }
        if(!$scope.user1.city){
            $scope.user1.city = $scope.user.city;
        }
        if(!$scope.user1.district){
            $scope.user1.district = $scope.user.district;
        }
        museumFac.product.editaddress($scope.user1,function(data){
            alert('保存成功');
            $scope.place1 = 1;
            $scope.getaddress();
          },function(data){
            alert(data.data.info);
        });
    }

    // 修改地址结束

    $scope.address_choose = function(list){
      $scope.pay_order.addressId = list.id;
      $scope.final_address = list;
      $scope.place1 = 0;

      if($scope.final_address.province == 1||$scope.final_address.province == 20||$scope.final_address.province == 860||$scope.final_address.province == 2462){
          $scope.city_show_name = $scope.final_address.province_name + ' ' + $scope.final_address.district_name;
      }else{
          $scope.city_show_name = $scope.final_address.province_name + ' ' + $scope.final_address.city_name + ' ' + $scope.final_address.district_name;
      }

    }

    $scope.pay = function(){
      museumFac.wechat.productpay($scope.pay_order, function(response) {
        var res = response;
        if (res.info) {
          alert(res.info);
          return;
        }

        var jsApiCall = function(data) {
          WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function(res) {
              WeixinJSBridge.log(res.err_msg);
                    // alert(res.err_code+res.err_desc+res.err_msg);
                if (res.err_msg == 'get_brand_wcpay_request:ok') {
                    if($scope.language == 0){
                      document.getElementById('title').innerHTML = '提示';
                      document.getElementById('windowclosebutton').value = '确定';
                      $scope.successtext = "付款成功！请至「我的订单」中关注订单状态。";
                    }else{
                      document.getElementById('title').innerHTML = 'Hint';
                      document.getElementById('windowclosebutton').value = 'Confirm';
                      $scope.successtext = "Purchase succeeded!";
                    }
                    alert($scope.successtext);
                    $state.go('tab.paysuccess');
                  } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                    if($scope.language == 0){
                      alert("已取消支付!");
                    }else{
                      alert("Payment canceled");
                    }

                    if($stateParams.id == 0){
                      $state.go('tab.order');
                    }
                    
                  } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                    $scope.guide = 0;
                    if($scope.language == 0){
                      alert("支付失败!");
                    }else{
                      alert("Payment failed");
                    }
                    if($stateParams.id == 0){
                      $state.go('tab.order');
                    }
                  } else {
                    $scope.guide = 0;
                    if($scope.language == 0){
                      alert('支付失败' + res.err_msg);
                    }else{
                      alert('Payment failed' + res.err_msg);
                    }
                    if($stateParams.id == 0){
                      $state.go('tab.order');
                    }
                  };
                }
              );
        }
        jsApiCall(res);
      }, function(data) {
        alert(data.data.info);
      });
    }

    $scope.payOrder = function() {

      if($scope.pay_order.addressId){
        $scope.pay_order.addressId = $scope.pay_order.addressId;
      }else{

        if($scope.order.address_id){
          $scope.pay_order.addressId = $scope.order.address_id;
        }else{
          if($scope.order.default_address){
            $scope.pay_order.addressId = $scope.order.default_address.id;
          }else{
            if(!$scope.pay_order.addressId){
                if($scope.language == 0){
                  alert("请选择收货地址");
                }else{
                  alert("Please choose a delivery address");
                }
            }
          }
          
        }
        
      }

      
      if($scope.pay_order.addressId){
        if($scope.payid != 0){
          $scope.pay_order.orderId = $scope.payid;

          if($scope.pay_order.orderId){
            $scope.pay();
          }

        }else{
          museumFac.product.cart(function(data){
              $scope.payid1 = data.info;
              $scope.pay_order.orderId = $scope.payid1;

              if($scope.pay_order.orderId){
                $scope.pay();
              }  

            },function(data){
              alert(data.data.info);
          });
        }
      } 
    }

    $scope.cancel_order = function(){
        museumFac.product.cancelorder({
          orderId:$stateParams.id
        },function(data){
            alert('已取消');
            $location.url('order');
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.cancel_place = function(){
      $scope.place1 = 0;
    }

    $scope.confirm = function(list){
       $scope.cancel_mark = 1;
       $scope.placeinfo = list;

       event.stopPropagation();
    }

    $scope.cancel_delete = function(){
       $scope.cancel_mark = 0;
    }

})